/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @headerfile i2c.hpp
* 
* Header for comunication through an I2C bus.
* 
* @author Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _MOKARDU_I2C_HPP_
#define _MOKARDU_I2C_HPP_

#include <linux/i2c-dev.h>
#include "debug.hpp"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>


/**
* @class CMI2c
* @brief Comunication through I2C busses.
*/

class CMI2c
{

	private:
	
		/**
		* The log class.
		*/
		CMDebug debug;
		
		/**
		* The sys where is the i2c device
		*/
		char* dev;
		
		/**
		* The address of the i2c device
		*/
		__u8 address;
		
		/**
		* Swap bytes on a word(2 bytes).
		*
		* @param[in] data The data to swap.
		* @return Returns the data swaped.
		*/
		__u16 swap_bytes(__u16 data);
		
		/**
		* A strlen function for an array of bytes.
		*
		* @param[in] data The data to compute.
		* @return Returns the size of the array.
		*/
		int strlen__u8(__u8* data);
		
	
	public:
	
		/**
		* Constructor.
		*
		* @param[in] dev The device for communication through I2C.
		* @param[in] address The address of the I2C device.
		* @param[in] debug If the debug is activated.
		* @param[in] filename The file for logging.
		*/
		CMI2c(const char* dev,__u8 address,bool debug,const char* filename);
		
		/**
		* Destructor.
		*/
		~CMI2c();
		
		/**
		* Connect to the I2C bus and send a block of characters.
		*
		* @param[in] data The data to send.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int sendBlock(__u8* data);
	
		/**
		* Connect to the DBUS bus and recive a word.
		*
		* @param[out] data The data received.
		* @param[in] command The command sended to device before receive data.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int recvWord(char* data,__u8 command);
		
};

#endif // _MOKARDU_I2C_HPP_
